import { formConf, inputComponents, systemComponents } from '@/generator/config'
import UcDictServiceApi from '@/service/publics/UcDictService';
import {
    deepClone
  } from '@/utils/formIndex'
export default {
    data(){
        return{
            jsonData:{
                formData: "",//表单数据
                columnData: "",//列表数据
            },
            formData:{
                ...formConf,
                fields:[]
            },
            columnData:{
                searchList:[],
                columnList:[],
                type:1,
            },

            formId: 101
        }
    },
    methods: {
        convertJson(viewItem,viewInfo){
            console.log('viewItem :>> ', viewItem);
            if(viewInfo.type === "tree"){
                this.columnData.type = 2;
                this.columnData.treePropsValue = viewInfo.idField;
                this.columnData.treeRelation = viewInfo.parentField;
            }else {
                this.columnData.type = 1;
            }

            // viewItem
            viewItem.attributes.forEach( async (element,index) => {
                let fields = {}
                if (element.referenceType == "dict") {//业务字典 级联选择
                    fields = await this.generateCascader(element,"cascader", index)
                }else if(element.referenceType == "view"){//主数据视图

                }else if(element.referenceType == "codeRule"){//编码规则

                }else if(element.referenceType == "defaultSysValue"){//默认系统值 系统控件
                    if (element.referenceData == "sysUser") {
                        fields = this.generateCreate(element,"createUser", index +1)
                    }else{
                        fields = this.generateCreate(element,"createTime", index+1)
                    }
                }else if(element.referenceType == "input" || !element.referenceType){//单行输入
                    if (element.dataType == "date") {
                        fields = this.generateInput(element,"date", index +1)
                    }else if (element.dataType == "number") {
                        fields = this.generateInput(element,"numInput", index+1)
                    }else{
                      fields = this.generateInput(element,"comInput", index+1)
                    }
                }
                //列表属性提取
                if(element.queryItem === "1"){
                    let item = {
                        columnName: element.code,
                        columnHumpName: element.code,
                        remarks: element.name,

                    };
                    this.columnData.searchList.push(item);
                }
                if(element.listView === "1"){
                    let item = {
                        columnName: element.code,
                        columnHumpName: element.code,
                        remarks: element.name,

                    };
                    this.columnData.columnList.push(item);
                }
                this.formData.fields.push(fields)
            });
            this.formData.idGlobal = this.formId
        },
        async generateCascader(element, type, index){
            let cascader = deepClone(this.filterComponents(type))
            cascader.vModel= element.code //字段编码
            cascader.config.label= element.name //字段名称
            cascader.config.dataType= "dictionary" 
            cascader.config.dictionaryType = element.referenceData
            cascader.required= element.canNull == 1 ? false : true //是否为空
            cascader.config.formId =  this.formId++
            cascader.config.tableName = element.linkTable
            await UcDictServiceApi.queryDict({nodeKey: element.referenceData}).then(res =>{
                res.data.forEach(item=>{
                    item.fullName = item.itemName
                })
              cascader.options = res.data
            })
              return cascader
        },
        generateInput(element, type, index){
            let comInput = deepClone(this.filterComponents(type))
            comInput.vModel= element.code //字段编码
            comInput.config.label= element.name //字段名称
            comInput.maxlength= element.dataLength //最大长度
            comInput.unique= element.isOnly == 1 ? false : true //是否唯一
            comInput.required= element.canNull == 1 ? false : true //是否为空
            comInput.config.formId = this.formId++
            comInput.config.tableName = element.linkTable
            // element.
           return comInput 
        },  
        generateCreate(element, type, index){
            let comInput = deepClone(this.filterSystemComponents(type))
            comInput.vModel= element.code //字段编码
            comInput.config.label= element.name //字段名称
            comInput.config.formId = this.formId++
            comInput.config.tableName = element.linkTable
           return comInput 
        },
        filterComponents(type) { 
            const arr1 =  inputComponents.filter(item =>
                item.config.pfKey == type
            )
            return arr1[0]
        },
        filterSystemComponents(type) { 
            const arr1 =  systemComponents.filter(item =>
                item.config.pfKey == type
            )
            return arr1[0]
        },
        clearData(){
            this.jsonData={
                formData: "",//表单数据
                columnData: "",//列表数据
            },
            this.formData={
                ...formConf,
                fields:[]
            },
            this.columnData={
                searchList:[],
                columnList:[],
                type:1,
            },

            this.formId= 101
        }
    }
}